package workspace.teaweb.Merchant.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import workspace.teaweb.Logistics.mappers.WarehouseMapper;
import workspace.teaweb.Logistics.pojo.dto.WarehouseDto2;
import workspace.teaweb.Logistics.pojo.entity.Warehouse;
import workspace.teaweb.Logistics.service.LogisticsService;
import workspace.teaweb.Logistics.service.impl.Logisticsimpl;
import workspace.teaweb.Merchant.mappers.MerchantMapper;
import workspace.teaweb.Merchant.pojo.dto.MerchantAddParam;
import workspace.teaweb.Merchant.pojo.dto.MerchantQuery;
import workspace.teaweb.Merchant.pojo.dto.MerchantUpdateParam;
import workspace.teaweb.Merchant.pojo.entity.Merchant;
import workspace.teaweb.Merchant.pojo.vo.MerchantVO;
import workspace.teaweb.Merchant.service.MerchantService;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MerchantServiceImpl implements MerchantService{
    @Autowired
    MerchantMapper merchantMapper;
    @Autowired
    Logisticsimpl logisticsimpl;

    @Override
    public void addMerchant(MerchantAddParam abc) {
        log.debug("添加商品popipipopapo:{}",abc);
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(abc,merchant);
        merchant.setCreateTime(new Date());
        log.debug("merchant={}",merchant);
        merchantMapper.addM(merchant);

        Warehouse warehouse = new Warehouse();
        warehouse.setMerchantid(merchant.getId());
        warehouse.setCreateTime(new Date());
        warehouse.setUpdateTime(new Date());
        warehouse.setNumber(abc.getNumber());
        logisticsimpl.AutoinsertWarehouse(warehouse);
    }

    @Override
    public List<MerchantVO> selectMerchant(MerchantQuery merchantQuery) {
        log.debug("查询商品参数:{}",merchantQuery);
        System.out.println("server层"+merchantQuery);
        List<MerchantVO> list = merchantMapper.selectMerchantByName(merchantQuery);

        return list;
    }

//    @Override
//    public List<MerchantVO> selectMerchantByUserId(Integer userId) {
//        log.debug("查询商品所用的商户id参数:{}",userId);
//        List<MerchantVO> list = merchantMapper.selectMerchantByUserId(userId);
//        return list;
//    }

@Autowired
WarehouseMapper WarehouseMapper;
    @Override
    public void updateMerchant(MerchantUpdateParam newMerchantMessage) {

        log.debug("更新商品参数:{}", newMerchantMessage);
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(newMerchantMessage, merchant);
        merchant.setUpdateTime(new Date());
        merchantMapper.updateMerchant(merchant);


        Warehouse warehouse = new Warehouse();
        BeanUtils.copyProperties(WarehouseMapper.selectWarehouseById(merchant.getId()),warehouse);
        warehouse.setNumber(newMerchantMessage.getNumber());
        System.out.println(warehouse);
        logisticsimpl.UserupdateWarehouse(warehouse);
    }

    @Override
    public void deleteMerchant(Long id) {
        log.debug("根据商品id删除商品业务:{}",id);
        merchantMapper.deleteMerchantById(id);
    }


}
